package com._21cn.product.utils;

import com._21cn.cms.common.Constants;
import com._21cn.cms.resource.utils.PictureUtil;
import com._21cn.cms.site.entity.RegionObject;
import com._21cn.cms.site.service.RegionObjectService;
import com._21cn.framework.utils.DateUtil;
import com._21cn.product.entity.*;
import com._21cn.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/19.
 */
@Component("watchRelateHelper")
public class WatchRelateHelper {

    @Autowired
    private CateService cateService;

    @Autowired
    private WatchPropertyService watchPropertyService;

    @Autowired
    private WatchCateService watchCateService;

    @Autowired
    private WatchPictureService watchPictureService;

    @Autowired
    private RegionObjectService regionObjectService;

    /**
     * 手表属性
     * @param propertyMap
     * @param watchId
     * @param typeId
     */
    public void insertPropertyMap(Map<Integer, String> propertyMap, Integer watchId, Integer typeId){
        updatePropertyMap(propertyMap, watchId, typeId);
    }

    public void updatePropertyMap(Map<Integer, String> propertyMap, Integer watchId, Integer typeId){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqWatchId", watchId);
        params.put("eqTypeId", typeId);
        watchPropertyService.delete(params);
        Date date = new Date();
        for(Integer key : propertyMap.keySet()){
            String propertyValue = propertyMap.get(key);
            insertWatchProperty(key, propertyValue, watchId, typeId, date);
        }
    }

    private void insertWatchProperty(Integer propertyId, String propertyValue, Integer watchId, Integer typeId, Date date){
        WatchProperty watchProperty = new WatchProperty();
        watchProperty.setPropertyId(propertyId);
        watchProperty.setTypeId(typeId);
        watchProperty.setWatchId(watchId);
        watchProperty.setPropertyValue(propertyValue);
        watchProperty.setCreateTime(date);
        watchProperty.setModifyTime(date);
        watchProperty.setStatus(Constants.STATUS_ENABLE);
        watchPropertyService.insert(watchProperty);
    }

    /**
     * 手表类别
     * @param cateIdList
     * @param watchId
     * @param typeId
     */
    public void updateWatchCateList(List<Integer> cateIdList, Integer watchId, Integer typeId){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqWatchId", watchId);
        params.put("eqTypeId", typeId);
        watchCateService.delete(params);
        insertWatchCateList(cateIdList, watchId, typeId);
    }

    public void insertWatchCateList(List<Integer> cateIdList, Integer watchId, Integer typeId){
        for(Integer cateId : cateIdList){
            insertWatchCate(cateId, watchId, typeId);
        }
    }

    private void insertWatchCate(Integer cateId, Integer watchId, Integer typeId){
        Date date = new Date();
        WatchCate watchCate = new WatchCate();
        watchCate.setModifyTime(date);
        watchCate.setCreateTime(date);
        watchCate.setWatchId(watchId);
        watchCate.setCateId(cateId);
        watchCate.setTypeId(typeId);
        watchCate.setStatus(Constants.STATUS_ENABLE);
        watchCateService.insert(watchCate);
    }

    public void updatePicList( List<String> picList, Integer watchId ){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqWatchId", watchId);
        watchPictureService.delete(params);
        insertPicList(picList, watchId);
    }

    public void insertPicList( List<String> picList, Integer watchId ){
        int i = 1;
        for (String picUrl : picList) {
            insertWatchPicture(watchId, picUrl, i);
            i++;
        }
    }

    private void insertWatchPicture(Integer watchId, String picUrl, int orderNum){
        Date date = new Date();
        WatchPicture watchPicture = new WatchPicture();
        watchPicture.setWatchId(watchId);
        watchPicture.setCreateTime(date);
        watchPicture.setPicUrl(picUrl);
        String hashCode = PictureUtil.getHashCodeFromUrl(watchPicture.getPicUrl());
        watchPicture.setPicHashcode(hashCode);
        watchPicture.setOrderNum(orderNum);
        watchPicture.setStatus(Constants.STATUS_ENABLE);
        watchPictureService.insert(watchPicture);
    }

    public void deleteWatchRelate(List<Integer> watchIdList){
        for( Integer watchId : watchIdList ){
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("eqWatchId", watchId);
            watchCateService.delete(params);
            watchPropertyService.delete(params);
            watchPictureService.delete(params);
        }
    }

    public void push2RegionWatch(Integer regionId, Integer objectId, Integer objectType){
        Integer topTime = DateUtil.getCurrent10TimeStamp();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqRegionId", regionId);
        params.put("eqObjectId", objectId);
        params.put("eqObjectType", objectType);
        List<RegionObject> list = regionObjectService.getList(params, 1, 1, false);
        if( !list.isEmpty()){
            RegionObject regionObject = list.get(0);
            regionObject.setModifyTime(new Date());
            regionObject.setTopTime(topTime);
            regionObjectService.update(regionObject);
        }else {
            RegionObject regionObject = new RegionObject();
            regionObject.setModifyTime(new Date());
            regionObject.setTopTime(topTime);
            regionObject.setCreateTime(new Date());
            regionObject.setObjectId(objectId);
            regionObject.setObjectType(objectType);
            regionObject.setRegionId(regionId);
            regionObject.setStatus(Constants.STATUS_ENABLE);
            regionObjectService.insert(regionObject);
        }

    }

}
